package com.wj.gulimall.order.controller;

import com.wj.gulimall.order.entity.OrderEntity;
import com.wj.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMQ/{num}")
    public String sendMQ(@PathVariable("num") Integer num) {
        for (int i = 0; i < num; i++) {
            //对象需要实现序列化接口
            if (i % 2 == 0) {
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("hahaha" + i);
                rabbitTemplate.convertAndSend(
                        "hello-java.exchange",
                        "hello.java",
                        reasonEntity,
                        new CorrelationData(UUID.randomUUID().toString())
                );
            } else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend(
                        "hello-java.exchange",
                        "hello22.java",
                        orderEntity,
                        new CorrelationData(UUID.randomUUID().toString())
                );
            }
        }
        return "ok";
    }
}
